﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomerOrderingSystem
{
    public partial class Invoice : Form
    {
        private string sql = "";
        public Invoice()
        {
            InitializeComponent();
        }

        private void Invoice_Load(object sender, EventArgs e)
        {
            invoiceDataGridView.DataSource = sql_invoice.QueryDataTable0();
        }

        private void MakeSqlStr()
        {
            {
                //清空上次的查询字符串
                sql = "";
                if (inoTextBox.Text.Trim() != string.Empty)
                {
                    sql = " and Ino like '%" + inoTextBox.Text.Trim() + "%'";
                }
                if (onoTextBox.Text.Trim() != string.Empty)
                {
                    sql += " and Ono like '%" + onoTextBox.Text.Trim() + "%'";
                }  
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MakeSqlStr();
            invoiceDataGridView.DataSource = sql_invoice.QueryDataTable(sql);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int cnt = sql_invoice.QueryDataTable1(inoTextBox1.Text);
                if (cnt == 1)
                {
                    MessageBox.Show("更新成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sql_invoice.QueryDataTable2(onoTextBox1.Text, paymentTextBox.Text, tatalTextBox.Text, dateTextBox.Text, inoTextBox1.Text);
                }
                // 添加新记录
                else
                {
                    MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK);
                    sql_invoice.QueryDataTable3(inoTextBox1.Text, onoTextBox1.Text, paymentTextBox.Text, tatalTextBox.Text, dateTextBox.Text);
                }
                Invoice_Load(null, null);
            }
            finally
            {
                sql_invoice.QueryDataTable4();
            }
            
                
                    
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult ret = MessageBox.Show("确定要删除记录吗？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (ret == DialogResult.Cancel)
            {
                return;
            }

            try
            {

                if (sql_invoice.QueryDataTable5(inoTextBox1.Text) == 1)
                {
                    MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Invoice_Load(null, null);
            }
            finally
            {
                sql_invoice.QueryDataTable4();
            }
            
        }
    }
}
